package com.yzh.oop9.oop9;

public class Test2 {
    public static void main(String[] args) {
        Book book1 = new Book("sdyxzh","123金庸","清华大学出版社",34.7);
        Book book2 = new Book("xajh","321梁羽生","清华大学出版社",34.1);
        System.out.println(book1.compareTo(book2));
        /*
        //局部内部类
        class BookNameComparator implements Comparator {
            @Override
            public int compare(Object obj1, Object obj2) {
                Book book1 =(Book)obj1;
                Book book2 =(Book)obj2;
                return book1.getBookName().compareTo(book2.getBookName());
            }
        }

        //使用外部比较器比较图书
        //Comparator comp1 = new BookNameComparator();
        */
        int num = 5;
        Comparator comp1 = new Comparator() {
            //代码块，每次创建对象时早于构造方法执行
            {
                System.out.println("===================");
            }
            @Override
            public int compare(Object obj1, Object obj2) {
                Book book1 =(Book)obj1;
                Book book2 =(Book)obj2;
                //num= 6;
                System.out.println(num);
                return book1.getBookName().compareTo(book2.getBookName());
            }
        };
        int n =comp1.compare(book1,book2);
        System.out.println(n);

        //如果局部内部类只用创建对象一次，就不使用了。还起个名字BookNameComparator是否有些多余呢？？？


        //Comparator comp2 = new BookNameComparator();


        //Comparator comp3 = new BookNameComparator();


    }
}
